<?php

namespace App\Http\Controllers;

use App\Repositories\StrategyRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Traits\BaseHttp;
use App\Models\StrategyModel;

class StrategyController extends BaseController
{
    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStrategyDesc (Request $request, StrategyRepository $strategyRepository)
    {
        $id = $request->get('id');
        $date = $request->get('date', '');
        $strategyDesc = @StrategyModel::where('id', $id)
            ->value('desc');
        $strategyDesc = $strategyRepository->replaceStrategyParams($strategyDesc, $date);
        return $this->success([
            'desc' => $strategyDesc
        ]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStrategyOptions ()
    {
        $user = request()->user();
        $strategies = @StrategyModel::select('id as value', 'name as label')
            ->where('admin_id', $user['id'])
            ->get();
        return $this->success($strategies);
    }

    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStrategyText (Request $request, StrategyRepository $strategyRepository)
    {
        $user = $request->user();
        $id = $request->get('id');
        $date = $request->get('date', '');
        $dateElliptical = $request->get('date_elliptical');
        $strategyDesc = @StrategyModel::where('id', $id)
            ->where('admin_id', $user['id'])
            ->value('desc');
        $strategyDesc = @$strategyRepository->replaceStrategyParams($strategyDesc, $date, $dateElliptical);
        if ($strategyDesc) {
            return $this->success($strategyDesc);
        } else {
            return $this->error('策略不存在');
        }
    }

    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function list (Request $request, StrategyRepository $strategyRepository)
    {
        $params = $request->all();
        $list = @$strategyRepository->getList($params);
        return $this->success($list);
    }

    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSttategyInfo (Request $request, StrategyRepository $strategyRepository)
    {
        $id = $request->get('id');
        $data = @$strategyRepository->getStrategyInfo($id);
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, StrategyRepository $strategyRepository)
    {
        $allParams = $request->all();
//        var_dump($allParams);
        $rst = @$strategyRepository->save($allParams);
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    /**
     * @param Request $request
     * @param StrategyRepository $strategyRepository
     */
    public function updateEarningRate (Request $request, StrategyRepository $strategyRepository)
    {
        $strategyId = $request->strategy_id;
        return @$strategyRepository->updateEarningRate($strategyId);
    }
}
